home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / JAVA / NET / URL.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  4.8 KB  |  237 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Hashtable;
  6. import java.util.StringTokenizer;
  7.  
  8. public final class URL {
  9.    private static final String protocolPathProp = "java.protocol.handler.pkgs";
  10.    private String protocol;
  11.    private String host;
  12.    private int port;
  13.    private String file;
  14.    private String ref;
  15.    URLStreamHandler handler;
  16.    static URLStreamHandlerFactory factory;
  17.    static Hashtable handlers = new Hashtable();
  18.  
  19.    public URL(String var1, String var2, int var3, String var4) throws MalformedURLException {
  20.       this.port = -1;
  21.       this.protocol = var1;
  22.       this.host = var2;
  23.       this.file = var4;
  24.       this.port = var3;
  25.       if ((this.handler = getURLStreamHandler(var1)) == null) {
  26.          throw new MalformedURLException("unknown protocol: " + var1);
  27.       }
  28.    }
  29.  
  30.    public URL(String var1, String var2, String var3) throws MalformedURLException {
  31.       this(var1, var2, -1, var3);
  32.    }
  33.  
  34.    public URL(String var1) throws MalformedURLException {
  35.       this((URL)null, var1);
  36.    }
  37.  
  38.    public URL(URL var1, String var2) throws MalformedURLException {
  39.       this.port = -1;
  40.       String var3 = var2;
  41.       int var7 = 0;
  42.       String var8 = null;
  43.  
  44.       try {
  45.          int var5;
  46.          for(var5 = var2.count; var5 > 0 && var2.charAt(var5 - 1) <= ' '; --var5) {
  47.          }
  48.  
  49.          while(var7 < var5 && var2.charAt(var7) <= ' ') {
  50.             ++var7;
  51.          }
  52.  
  53.          if (var2.regionMatches(true, var7, "url:", 0, 4)) {
  54.             var7 += 4;
  55.          }
  56.  
  57.          char var6;
  58.          for(int var4 = var7; var4 < var5 && (var6 = var2.charAt(var4)) != '/'; ++var4) {
  59.             if (var6 == ':') {
  60.                var8 = var2.substring(var7, var4).toLowerCase();
  61.                var7 = var4 + 1;
  62.                break;
  63.             }
  64.          }
  65.  
  66.          if (var1 == null || var8 != null && !var8.equals(var1.protocol)) {
  67.             this.protocol = var8;
  68.          } else {
  69.             this.protocol = var1.protocol;
  70.             this.host = var1.host;
  71.             this.port = var1.port;
  72.             this.file = var1.file;
  73.          }
  74.  
  75.          if (this.protocol == null) {
  76.             throw new MalformedURLException("no protocol: " + var3);
  77.          } else if ((this.handler = getURLStreamHandler(this.protocol)) == null) {
  78.             throw new MalformedURLException("unknown protocol: " + this.protocol);
  79.          } else {
  80.             int var12 = var2.indexOf(35, var7);
  81.             if (var12 >= 0) {
  82.                this.ref = var2.substring(var12 + 1, var5);
  83.                var5 = var12;
  84.             }
  85.  
  86.             this.handler.parseURL(this, var2, var7, var5);
  87.          }
  88.       } catch (MalformedURLException var10) {
  89.          throw var10;
  90.       } catch (Exception var11) {
  91.          throw new MalformedURLException(var2 + ": " + var11);
  92.       }
  93.    }
  94.  
  95.    protected void set(String var1, String var2, int var3, String var4, String var5) {
  96.       this.protocol = var1;
  97.       this.host = var2;
  98.       this.port = var3;
  99.       this.file = var4;
  100.       this.ref = var5;
  101.    }
  102.  
  103.    public int getPort() {
  104.       return this.port;
  105.    }
  106.  
  107.    public String getProtocol() {
  108.       return this.protocol;
  109.    }
  110.  
  111.    public String getHost() {
  112.       return this.host;
  113.    }
  114.  
  115.    public String getFile() {
  116.       return this.file;
  117.    }
  118.  
  119.    public String getRef() {
  120.       return this.ref;
  121.    }
  122.  
  123.    public boolean equals(Object var1) {
  124.       return var1 instanceof URL && this.sameFile((URL)var1);
  125.    }
  126.  
  127.    public int hashCode() {
  128.       int var1 = 0;
  129.       if (!this.host.equals("")) {
  130.          try {
  131.             InetAddress var2 = InetAddress.getByName(this.host);
  132.             var1 = var2.address;
  133.          } catch (UnknownHostException var3) {
  134.          }
  135.       }
  136.  
  137.       return this.protocol.hashCode() ^ var1 ^ this.file.hashCode();
  138.    }
  139.  
  140.    boolean hostsEqual(String var1, String var2) {
  141.       if (var1.equals(var2)) {
  142.          return true;
  143.       } else {
  144.          try {
  145.             InetAddress var3 = InetAddress.getByName(var1);
  146.             InetAddress var4 = InetAddress.getByName(var2);
  147.             if (var4 != null && var4 instanceof InetAddress && (var4).address == var3.address) {
  148.                return true;
  149.             }
  150.  
  151.             return false;
  152.          } catch (UnknownHostException var6) {
  153.          } catch (SecurityException var7) {
  154.          }
  155.  
  156.          return false;
  157.       }
  158.    }
  159.  
  160.    public boolean sameFile(URL var1) {
  161.       return this.protocol.equals(var1.protocol) && this.hostsEqual(this.host, var1.host) && this.port == var1.port && this.file.equals(var1.file);
  162.    }
  163.  
  164.    public String toString() {
  165.       return this.handler.toExternalForm(this);
  166.    }
  167.  
  168.    public String toExternalForm() {
  169.       return this.handler.toExternalForm(this);
  170.    }
  171.  
  172.    public URLConnection openConnection() throws IOException {
  173.       return this.handler.openConnection(this);
  174.    }
  175.  
  176.    public final InputStream openStream() throws IOException {
  177.       return this.handler.openConnection(this).getInputStream();
  178.    }
  179.  
  180.    public final Object getContent() throws IOException {
  181.       return this.handler.openConnection(this).getContent();
  182.    }
  183.  
  184.    public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory var0) {
  185.       if (factory != null) {
  186.          throw new Error("factory already defined");
  187.       } else {
  188.          SecurityManager var1 = System.security;
  189.          if (var1 != null) {
  190.             var1.checkSetFactory();
  191.          }
  192.  
  193.          factory = var0;
  194.       }
  195.    }
  196.  
  197.    static synchronized URLStreamHandler getURLStreamHandler(String var0) {
  198.       String var1 = var0;
  199.       if (var0.equals("https")) {
  200.          var1 = new String("http");
  201.       }
  202.  
  203.       URLStreamHandler var2 = (URLStreamHandler)handlers.get(var1);
  204.       if (var2 == null) {
  205.          if (factory != null) {
  206.             var2 = factory.createURLStreamHandler(var1);
  207.          }
  208.  
  209.          if (var2 == null) {
  210.             String var3 = System.getProperty("java.protocol.handler.pkgs", "");
  211.             if (var3 != "") {
  212.                var3 = var3 + "|";
  213.             }
  214.  
  215.             var3 = var3 + "sun.net.www.protocol";
  216.             StringTokenizer var4 = new StringTokenizer(var3, "|");
  217.  
  218.             while(var2 == null && var4.hasMoreTokens()) {
  219.                String var5 = var4.nextToken().trim();
  220.  
  221.                try {
  222.                   String var6 = var5 + "." + var1 + ".Handler";
  223.                   var2 = (URLStreamHandler)Class.forName(var6).newInstance();
  224.                } catch (Exception var7) {
  225.                }
  226.             }
  227.          }
  228.  
  229.          if (var2 != null) {
  230.             handlers.put(var1, var2);
  231.          }
  232.       }
  233.  
  234.       return var2;
  235.    }
  236. }
  237.